package net.example.activity.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.example.manager.ClientGlobalCache;
import net.example.model.GGUser;
import net.example.model.UserStatus;
import net.example.ggtalk.R;
import net.example.utils.CommonHelper;
import net.example.utils.CommonOptions;
import net.example.utils.HeadImgHelper;
import net.example.utils.android.AndroidUI;
import net.example.activity.view.sessionview.MessageItem;
import net.example.activity.view.sessionview.SlidingDeleteSlideView;
import net.example.activity.view.sessionview.SlidingDeleteSlideView.OnSlideListener;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;

public class SessionAdapter extends BaseAdapter {

	private Context context;
	private List<MessageItem> mMessageItems;
	private OnSlideListener onSlideListener;
	private OnDeleteListener onDeleteListen;
	private boolean isLongState;
	private HashMap<Integer, Boolean> checkedItemMap = new HashMap<Integer, Boolean>();
	private Drawable longStateItemCheckedBg;
	private Calendar calendar;

	public SessionAdapter(Context context, List<MessageItem> mMessageItems,
                          OnSlideListener onSlideListener,
                          OnDeleteListener onDeleteListen) {
		super();
		this.context = context;
		this.mMessageItems = mMessageItems;
		this.onSlideListener = onSlideListener;
		this.onDeleteListen = onDeleteListen;
		longStateItemCheckedBg = context.getResources().getDrawable(
				R.drawable.xlistview_check_bg);
		calendar=Calendar.getInstance();
	}

	@Override
	public int getCount() {
		return mMessageItems.size();
	}

	@Override
	public Object getItem(int position) {
		return mMessageItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, final View convertView,
			ViewGroup parent) {
		ViewHolder holder;
		SlidingDeleteSlideView slideView = (SlidingDeleteSlideView) convertView;

		if (slideView == null) {
			View itemView = LayoutInflater.from(context).inflate(
					R.layout.group_item, parent, false);
			slideView = new SlidingDeleteSlideView(context);
			slideView.setContentView(itemView);

			holder = new ViewHolder(slideView);
			slideView.setOnSlideListener(onSlideListener);
			slideView.setTag(holder);
		} else {
			holder = (ViewHolder) slideView.getTag();
		}
		MessageItem item = mMessageItems.get(position);
		item.slideView = slideView;
		item.slideView.shrinkByFast();

		if (isLongState) {
			if (checkedItemMap.containsKey(position)) {
				holder.xlist_item_relayout
						.setBackgroundDrawable(longStateItemCheckedBg);
			} else {
				holder.xlist_item_relayout.setBackgroundColor(0xFFFFFFFF);
			}
		}
		if(item.isGroup)
		{
			holder.icon.setImageResource(R.drawable.group_ico);
		}
		else {
			GGUser GGUser = ClientGlobalCache.getInstance().getUser(item.senderID);
			HeadImgHelper.setUserHeadImg(holder.icon, GGUser);
		}

		UserStatus status=  item.userStatus;
		if(item.isGroup) {
			UserStatus.setImageBackgroundGrayByStatus(UserStatus.Online, holder.icon,holder.statusView);
			holder.statusView.setVisibility(View.GONE);
			holder.title.setText(item.title);
		}else {
			if(item.senderID.equals(CommonOptions.ValidationMessageID))//验证消息单独处理
			{
				holder.icon.setImageResource(R.drawable.verify);
				UserStatus.setImageBackgroundGrayByStatus(UserStatus.Online, holder.icon, holder.statusView);
				holder.statusView.setVisibility(View.GONE);
				holder.title.setText(item.title);
			}else {
				holder.statusView.setImageResource(UserStatus.getUserStatusResource(status, false));
				UserStatus.setImageBackgroundGrayByStatus(status, holder.icon, holder.statusView);
				holder.title.setText(ClientGlobalCache.getInstance().getUser(item.senderID).getCommentName());
			}
		}
		if (item.statusChanged) {
			AndroidUI.animationAlpha(holder.xlist_item_relayout);
			item.statusChanged = false;
		}
		if (item.unReadCount >0) {
			holder.res.setVisibility(View.VISIBLE);
			holder.res.setText(CommonHelper.getMaxCountStr4Show(item.unReadCount) );
			//AndroidUI.shakeAnimationVertical(Animation.INFINITE, holder.xlist_item_relayout);去掉新消息抖动
		} else{
			holder.res.setVisibility(View.INVISIBLE);
			//AndroidUI.clearAnimation(holder.xlist_item_relayout);
		}


		holder.msg.setText(item.lastWord);

		calendar.setTime(new Date());
		Calendar today =  GregorianCalendar.getInstance();//calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1,calendar.get(Calendar.DATE));
		today.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DATE),0,0,0);
		String timeStr="";
		SimpleDateFormat dateFormat=null;//new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		if(item.time.after(today.getTime()))
		{
			dateFormat=new SimpleDateFormat("HH:mm");
		}else {
			dateFormat=new SimpleDateFormat("MM月dd日");
		}
		timeStr= dateFormat.format(item.time);
		holder.last_message_time.setText(timeStr);
		holder.deleteHolder.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				onDeleteListen.onDelete(convertView, position);
			}
		});

		return slideView;
	}

	private static class ViewHolder {
		public RelativeLayout xlist_item_relayout;
		public ImageView icon,statusView;
		public TextView title;
		public TextView msg;
		public TextView res;//未读消息
		public TextView last_message_time;//消息时间
		public ViewGroup deleteHolder;

		ViewHolder(View view) {
			xlist_item_relayout = (RelativeLayout) view
					.findViewById(R.id.xlist_item_relayout);
			xlist_item_relayout.setId(View.generateViewId());
			icon = (ImageView) view.findViewById(R.id.icon);
			title = (TextView) view.findViewById(R.id.title);
			statusView = (ImageView) view.findViewById(R.id.ct_status);
			msg = (TextView) view.findViewById(R.id.msg);
			res = (TextView) view.findViewById(R.id.res);
			last_message_time=(TextView)view.findViewById(R.id.last_message_time);
			deleteHolder = (ViewGroup) view.findViewById(R.id.holder);
		}
	}

	public interface OnDeleteListener {
		void onDelete(View view, int position);
	}

	public void setIsLongState(boolean isLongState) {
		this.isLongState = isLongState;
	}

	public void setCheckItemMap(HashMap<Integer, Boolean> checkedItemMap) {
		this.checkedItemMap = checkedItemMap;
	}

}
